<?php

namespace common\models\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Member;

/**
 * MemberSearch represents the model behind the search form about `common\models\Member`.
 */
class MemberSearch extends Member
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['member_id', 'member_sex', 'member_login_num', 'member_goldnum', 'member_goldnumcount', 'member_goldnumminus', 'member_points', 'inform_allow', 'is_buy', 'is_allowtalk', 'member_state', 'member_credit'], 'integer'],
            [['member_name', 'member_truename', 'member_avatar', 'member_birthday', 'member_password', 'member_email', 'member_time', 'member_login_time', 'member_old_login_time', 'member_login_ip', 'member_old_login_ip'], 'safe'],
            [['available_predeposit', 'freeze_predeposit'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
// bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Member::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
// uncomment the following line if you do not want to any records when validation fails
// $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'member_id'            => $this->member_id,
            'member_sex'           => $this->member_sex,
            'member_birthday'      => $this->member_birthday,
            'member_login_num'     => $this->member_login_num,
            'member_goldnum'       => $this->member_goldnum,
            'member_goldnumcount'  => $this->member_goldnumcount,
            'member_goldnumminus'  => $this->member_goldnumminus,
            'member_points'        => $this->member_points,
            'available_predeposit' => $this->available_predeposit,
            'freeze_predeposit'    => $this->freeze_predeposit,
            'inform_allow'         => $this->inform_allow,
            'is_buy'               => $this->is_buy,
            'is_allowtalk'         => $this->is_allowtalk,
            'member_state'         => $this->member_state,
            'member_credit'        => $this->member_credit,
        ]);

        $query->andFilterWhere(['like', 'member_name', $this->member_name])
            ->andFilterWhere(['like', 'member_truename', $this->member_truename])
            ->andFilterWhere(['like', 'member_avatar', $this->member_avatar])
            ->andFilterWhere(['like', 'member_password', $this->member_password])
            ->andFilterWhere(['like', 'member_email', $this->member_email])
            ->andFilterWhere(['like', 'member_time', $this->member_time])
            ->andFilterWhere(['like', 'member_login_time', $this->member_login_time])
            ->andFilterWhere(['like', 'member_old_login_time', $this->member_old_login_time])
            ->andFilterWhere(['like', 'member_login_ip', $this->member_login_ip])
            ->andFilterWhere(['like', 'member_old_login_ip', $this->member_old_login_ip]);

        return $dataProvider;
    }
}